package com.a.l;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import javax.security.auth.callback.Callback;

import static android.content.Context.BIND_AUTO_CREATE;

public class JwtDomain {
    private String Tag = "JwtDomain";
    private Context mContext;
    private static JwtDomain sJwtDomain;
    private JwtCallback mCallback;
    private IKonkeAidlInterface mIKonkeAidlInterface;
    private boolean mInitSucess;
    private JWTReq mJwtReq;

    private JwtDomain(Context context) {
        mContext = context;
    }

    public static JwtDomain getInstance(Context context) {
        if (sJwtDomain == null) {
            sJwtDomain = new JwtDomain(context);
        }
        return sJwtDomain;
    }

    private boolean init() {
        Intent intent = new Intent(IKonkeAidlInterface.class.getName());
        intent.setAction("konkeServer");
//        intent.setPackage("com.a.l");com.ikonke.huarun_pad_app  com.ikonke.huarun_pad_app  com.example.test

        intent.setComponent(new ComponentName("com.ikonke.huarun_pad_app", "com.a.l.KonkeServer"));
        boolean b = mContext.bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
        Log.e("1", b + "");
        mInitSucess = b;
        return b;
    }

    public boolean addJWTFromCallBack(JWTReq jwtReq, JwtCallback callback) {
        mJwtReq = jwtReq;
        mCallback = callback;
        if (mInitSucess) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        mIKonkeAidlInterface.addJWTFromCallback(jwtReq, mStub);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        boolean init = init();
        if (!init) {
            return false;
        }
        return true;
    }

    public boolean addJWTFromCallBack() {
        if (mJwtReq == null) {
            Log.e(Tag,"addJWTFromCallBack mJwtReq = null ");
            return false;
        }
        if (mInitSucess) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (mIKonkeAidlInterface != null) {
                            mIKonkeAidlInterface.addJWTFromCallback(mJwtReq, mStub);
                        }
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        boolean init = init();
        if (!init) {
            return false;
        }
        return true;
    }



    public JWTResp getJWTResp(JWTReq jwtReq) throws RemoteException {
        if (!mInitSucess) {
            Toast.makeText(mContext, "未初始化", Toast.LENGTH_LONG);
            Log.e(Tag, "mIKonkeAidlInterface =null  aidl not connected ");
            return null;
        }
        if (mIKonkeAidlInterface != null) {
            return mIKonkeAidlInterface.getJwt(jwtReq);
        }
        return null;
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e(Tag, "onServiceConnected");
            mIKonkeAidlInterface = IKonkeAidlInterface.Stub.asInterface(iBinder);
            if (mCallback != null) {
                try {
                    mIKonkeAidlInterface.addJWTFromCallback(mJwtReq, mStub);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(Tag, "onServiceDisconnected");
        }
    };

    private IKonkeAidlCallback.Stub mStub = new IKonkeAidlCallback.Stub() {
        @Override
        public void result(JWTResp resp) throws RemoteException {
            if (mCallback != null) {
                mCallback.onUpdate(resp);
            }
        }
    };

    public void destory() throws RemoteException {
        if (mIKonkeAidlInterface != null) {
            mContext.unbindService(mServiceConnection);
        }
    }

}
